package com.hyh.elasticsearch.config;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;

/**
 * RestClientConfig
 *
 * @Author: heyuhua
 * @Date: 2021/2/3 15:26
 */
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class RestClientConfig extends AbstractElasticsearchConfiguration {

    /**
     * host name
     */
    private String host;
    /**
     * port
     */
    private String port;

    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {

        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo(host + ":" + port)
                .build();

        return RestClients.create(clientConfiguration).rest();
    }

}
